package com.shushan.accessibility.emui;

import android.content.Context;
import android.util.Log;

import com.android.permission.rom.RomUtils;
import com.shushan.accessibility.AccessibilityOperator;


/**
 * @ClassName: EmuiOperator
 * @Desciption: EMUI辅助功能操作类
 * @author: yichaohua
 * @date: 2018-07-05
 */
public class EmuiOperator {

    private static final String TAG = "EmuiOperator";

    private static EmuiOperator mInstance = new EmuiOperator();

    public static EmuiOperator getInstance() {
        return mInstance;
    }

    public boolean closeSavePower(Context context) {

        if(RomUtils.getEmuiLeval(context) >= 14){
            boolean result = AccessibilityOperator.getInstance().clickListItem
                    ("com.android.settings:id/dashboard_container","电池",1);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                result = AccessibilityOperator.getInstance().clickById("com.huawei.systemmanager:id/background_consume");
                if(!result){
                    return false;
                }else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    result = AccessibilityOperator.getInstance().clickListReSwitch
                            ("com.huawei.systemmanager:id/lv_listview","书山宝",1,false);
                    Log.i(TAG,"closeSavePower() result=" + result);
//                    if(!result){
//                        return false;
//                    }else {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        result = AccessibilityOperator.getInstance().clickListSwitch("允许自启动",1,true);
                        if(!result){
                            return false;
                        }else {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            result = AccessibilityOperator.getInstance().clickListSwitch("允许后台活动",1,true);
                            if(!result){
                                return false;
                            }else {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                return AccessibilityOperator.getInstance().clickById("android:id/button1");
                            }
                        }


//                    }
                }
            }

        }else if(RomUtils.getEmuiLeval(context) >= 11){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean result = AccessibilityOperator.getInstance().clickListSwitch
                    ("com.huawei.systemmanager:id/progress_manager_white_gridview","书山宝",1,false);
            if(result){
                return true;
            }else {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return AccessibilityOperator.getInstance().clickListSwitch
                        ("com.huawei.systemmanager:id/progress_manager_white_gridview","书山宝",1,false);
            }
        }else {
            return AccessibilityOperator.getInstance().clickListSwitch
                    ("com.huawei.systemmanager:id/progress_manager_white_list","书山宝",1,true);
        }

    }

    public boolean openComplete(Context context) {

        if(RomUtils.getEmuiLeval(context) >= 14){

        }else if(RomUtils.getEmuiLeval(context) >= 11){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean result = AccessibilityOperator.getInstance().clickListItemSwitch
                    ("com.huawei.systemmanager:id/startupmgr_main_grid_view","书山宝",1,true);
            Log.i(TAG,"openComplete() result=" + result);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return AccessibilityOperator.getInstance().clickById("android:id/button1");
            }
        }else {
            boolean result = AccessibilityOperator.getInstance().clickListSwitch
                    ("com.huawei.systemmanager:id/startupmgr_main_list_view","书山宝",1,true);
            Log.i(TAG,"openComplete() result=" + result);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return AccessibilityOperator.getInstance().clickById("android:id/button1");
            }
        }

        return false;
    }

    public boolean openFloatManager() {
        return AccessibilityOperator.getInstance().clickOnlySwitch(true);
    }

    public boolean openUsageSetting() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean result = AccessibilityOperator.getInstance().clickListItem("书山宝",1);
        Log.i(TAG,"openUsageSetting() result=" + result);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return AccessibilityOperator.getInstance().clickOnlySwitch(true);
        }
    }

    public boolean enableAdmin(Context context) {
        if(RomUtils.getEmuiLeval(context) >= 14){
            return AccessibilityOperator.getInstance().clickById("com.android.settings:id/restricted_action");
        }else if(RomUtils.getEmuiLeval(context) >= 11){
            return AccessibilityOperator.getInstance().clickById("com.android.settings:id/action_button");
        }else {
            return AccessibilityOperator.getInstance().clickByText("激活");
        }

    }
}
